import {BrowserWindow, Menu, Tray, app, shell} from "electron";
import {trayURL, trayIconPath, trayTransparentIconPath} from "../config/staticPath";
import pkg from "../../../package.json";
import menu, {createTrayWindow, sleep} from "../hook/trayHook";

let tray: Tray

export function initTray() {
	// console.log('trayIconPath', trayIconPath)
	tray = new Tray(trayIconPath);

	tray.setToolTip(app.name)
	tray.setTitle(pkg.title)

	if (process.platform === 'darwin') {
		const trayWin = createTrayWindow()
		tray.on('click', (e, bounds, position) => {
			trayWin.setPosition(bounds.x, bounds.y)
			trayWin.show()
		})
	} else {
		tray.setContextMenu(menu)
	}

	tray.on('double-click', async () => {
		let times = 3
		while (times > 0) {
			shell.beep()
			tray.setImage(trayTransparentIconPath)
			await sleep(500)
			tray.setImage(trayIconPath)
			await sleep(500)
			times--
		}
	})

	return tray
}
